package net.koodar.forge.system.dept.manager;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.koodar.forge.common.exception.BizException;
import net.koodar.forge.system.dept.model.entity.Dept;
import net.koodar.forge.system.dept.dao.DeptMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liyc
 */
@Service
public class DeptManager extends ServiceImpl<DeptMapper, Dept> {

    public long countByParentIdAndDeptName(Long parentId, String deptName) {
        return baseMapper.selectCount(Wrappers.<Dept>lambdaQuery().eq(Dept::getParentId, parentId).eq(Dept::getDeptName, deptName));
    }

    public Dept findByParentIdAndDeptName(Long parentId, String deptName) {
        List<Dept> deptList = baseMapper.selectList(Wrappers.<Dept>lambdaQuery().eq(Dept::getParentId, parentId).eq(Dept::getDeptName, deptName));
        return deptList.isEmpty() ? null : deptList.get(0);
    }

    public Dept findById(Long id) {
        Dept dept = baseMapper.selectById(id);
        if (dept == null) {
            throw new BizException(String.format("部门id[%s]不存在", id));
        }
        return null;
    }
}
